var util = require('/utils/util.js')
//app.js
App({
  onLaunch: function () {
    // 展示本地存储能力
    var logs = wx.getStorageSync('logs') || []
    logs.unshift(Date.now())
    wx.setStorageSync('logs', logs)
	 
	 //登录
	 this.wxlogin()
  },

  globalData: {
	  rootDocment: 'https://xiaohongdai.net/',
	  ufurl:'Home/Login/index',
    userInfo: null,
	  nickName: null,	//	昵称	
	  userID: 0,	//	用户ID
	  userPhone: null,	// 用户手机号
	  userLogType: false,	//	账号登录状态
	  userInfoId: 0,	//	账号ID
	  loginButton: true,	//	个人中心是否显示登录按钮
	  personalHeaderName: false,	//	个人中心是否显示信息
	  switchtype: false,	//是否允许跳转
		continuoustype: true,	//是否禁止连续打开页面
    scodeval: null,//扫码进入的参数
    unitprice:0, //下单支付单价
  },
  //统一登录验证方法
  userlogin: function () {
	  var thats = this
	  //获取用户当前的授权状态
	  wx.getSetting({
		  success: function(res){
			  if (res.authSetting['scope.userInfo']) {
				  //获取用户信息
				  thats.getUserInfo()
			  } else { //授权状态关闭
				  if (res.authSetting['scope.userInfo'] == null) { //未发起授权
					  //登录
					  thats.getUserInfo()
				  } else { //拒绝授权
					  wx.openSetting({
						  success: (res) => {
							  if (res.authSetting['scope.userInfo']) { //重新授权成功
								  //获取用户信息
								  thats.getUserInfo()
							  }else{//退回
								  thats.globalData.switchtype = false
							  }
						  }
					  })
				  }
			  }

		  }
		  
	  })
  },
  getUserInfo: function () { //获取用户信息
	  var thats = this
	  //获取用户信息
	  wx.getUserInfo({
		  success: res => {
			  // 可以将 res 发送给后台解码出 unionId
			  thats.globalData.userInfo = res.userInfo  //设置全局用户信息
			  thats.globalData.switchtype = true
			  // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
			  // 所以此处加入 callback 以防止这种情况
			  if (this.userInfoReadyCallback) {
				  this.userInfoReadyCallback(res)
			  }
		  }
	  })
  },
  relogin: function () {
	  wx.login({
		  success: res => {
			  // 发送 res.code 到后台换取 openId, sessionKey, unionId
			  if (res.code) {
				  //发起网络请求
				  util.AJAX('Home/Login/index', { code: res.code }, function (ress) { //方法体
					  //存储setStorage
					  wx.setStorageSync('sessionid', ress.data.sessionid)
				  }, 'POST')
			  } else {
				  console.log('获取用户登录态失败！' + res.errMsg)
			  }
		  }
	  })
	  //获取用户信息
	  this.userlogin()
  },
  globalshare: function () {  //统一分享
    var that = this
	  return {
		  title: '小洪干洗',
		  path: '/pages/index/index',
      imageUrl: that.globalData.rootDocment + 'Public/Home/images/shareImg.jpg',
		  success: function (res) {
			  // 转发成功
		  },
		  fail: function (res) {
			  // 转发失败
		  }
	  }
  },
  wxlogin: function () {	//统一登录方法
	  var thats = this
	  //检测用户登录态是否失效
	  wx.checkSession({
		  success: function () {
			  //session 未过期，并且在本生命周期一直有效
			  //获取本地存储信息
			  var sessvalue = wx.getStorageSync('sessionid')
			  if (sessvalue) { //如果存在
				  //去服务端验证是否过期
				  util.AJAX('Home/Login/getsess', { 'sesskey': sessvalue }, function (res) { //方法体
					  if (res.data.StatusCode == 100){  //存在key
						  thats.getUserInfo()
					  }
					  if (res.data.StatusCode == 200){  //登录失败
						  thats.relogin()
					  }
					  if (res.data.StatusCode == 300) {	//参数错误
						  wx.showModal({
							  title: '提示',
							  showCancel: false,
							  content: res.data.Msg,
							  success: function (res) {
								  if (res.confirm) {
									  console.log('用户点击确定')
								  }
							  }
						  })
					  }
				  }, 'POST')
			  } else {
				  //重新登录
				  thats.relogin()
			  }
		  },
		  fail: function () {
			  //登录态过期，重新登录
			  thats.relogin()
			}
		})
	},
   checkLogin: function () {  //判断是否登录
	  if(this.globalData.userLogType == true && this.globalData.userInfoId != ''){ //登录中
			return true
	  }else{
			return false
	  }
   },
	navigateTologing: function () {  //跳转到登录
		/*wx.navigateTo({
			url: '/pages/login/login',
		})*/
		this.publicnavigateTologing('/pages/login/login')
	},
	logout: function () {  //退出登录
		this.globalData.loginButton = true
		this.globalData.nickName = null
		this.globalData.personalHeaderName = false
		this.globalData.switchtype = true
		this.globalData.userID = 0
		this.globalData.userInfoId = 0
		this.globalData.userLogType = false
		this.globalData.userPhone = null

    wx.setStorageSync('userloginfo', '')
	},
	//公共跳转
	publicnavigateTologing: function (urlstr) {
		var that = this
		if (that.globalData.continuoustype == true) {
			that.globalData.continuoustype = false
			setTimeout(function () {
				wx.navigateTo({
					url: urlstr,
				})
			}, 300)
		}
	},
	//公共更改跳转状态
	publicchangenavigate: function () {
		var that = this
		setTimeout(function () {
			that.globalData.continuoustype = true
		}, 300)
	}
})